/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPenerimaanPembelian.java
 *
 * Created on May 9, 2011, 11:55:43 PM
 */

package com.kifas.ui.purchase;

import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.PembelianDetailModel;
import com.kifas.model.xml.PembelianModel;
import com.kifas.model.xml.PenerimaanDetailModel;
import com.kifas.model.xml.PenerimaanModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.ReferenceNumberingModel;
import com.kifas.service.MasterService;
import com.kifas.service.PenerimaanService;
import com.kifas.ui.master.LookUpGudang;
import com.kifas.ui.util.TextComponentUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author dhamarsu
 */
public class FormPenerimaanPembelian extends javax.swing.JInternalFrame {
    private GudangModel gudangModel;
    private PembelianModel pembelianModel;
    private List<PembelianDetailModel> detailPembelianModels;
    private List<PenerimaanDetailModel> penerimaanDetailModels;
    private PenerimaanModel penerimaanModel;
    private PenerimaanDetailModel penerimaanDetailModel;
    private PembelianModel p;
    private PenerimaanService penerimaanService = new PenerimaanService();
    private MasterService masterService = new MasterService();
    
    /** Creates new form FormPenerimaanPembelian */
    public FormPenerimaanPembelian() {
        initComponents();

        tableOrder.setAutoCreateColumnsFromModel(false);
        detailPembelianModels = new ArrayList<PembelianDetailModel>();
        tableOrder.setModel(new PembelianTableModel(detailPembelianModels));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNoPo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dtPenerimaan = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        txtSupplier = new javax.swing.JTextField();
        btnOrder = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtBLNumber = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNoPenerimaan = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtGudang = new javax.swing.JTextField();
        btnGudang = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btnSave = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnBrowse = new javax.swing.JButton();
        btnDo = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableOrder = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtSubTotal = new javax.swing.JLabel();
        txtPajak = new javax.swing.JLabel();
        txtTotal = new javax.swing.JLabel();
        txtDiscount = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtKeterangan = new javax.swing.JTextArea();

        setClosable(true);
        setIconifiable(true);
        setTitle("Form Penerimaan Pembelian");

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("No Order");
        jLabel1.setName("jLabel1"); // NOI18N

        txtNoPo.setName("txtNoPo"); // NOI18N

        jLabel2.setText("Tanggal");
        jLabel2.setName("jLabel2"); // NOI18N

        dtPenerimaan.setName("dtPenerimaan"); // NOI18N

        jLabel3.setText("Supplier");
        jLabel3.setName("jLabel3"); // NOI18N

        txtSupplier.setName("txtSupplier"); // NOI18N

        btnOrder.setText("...");
        btnOrder.setName("btnOrder"); // NOI18N
        btnOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrderActionPerformed(evt);
            }
        });

        jLabel5.setText("BL Number");
        jLabel5.setName("jLabel5"); // NOI18N

        txtBLNumber.setName("txtBLNumber"); // NOI18N

        jLabel4.setText("No Penerimaan");
        jLabel4.setName("jLabel4"); // NOI18N

        txtNoPenerimaan.setText("AUTO");
        txtNoPenerimaan.setName("txtNoPenerimaan"); // NOI18N

        jLabel6.setText("Gudang");
        jLabel6.setName("jLabel6"); // NOI18N

        txtGudang.setName("txtGudang"); // NOI18N

        btnGudang.setText("...");
        btnGudang.setName("btnGudang"); // NOI18N
        btnGudang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGudangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(59, 59, 59)
                        .addComponent(txtNoPo, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtNoPenerimaan)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dtPenerimaan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtSupplier, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtGudang)
                    .addComponent(txtBLNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGudang, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNoPo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnOrder)
                            .addComponent(jLabel5)
                            .addComponent(txtBLNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txtSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(txtGudang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnGudang))
                            .addComponent(dtPenerimaan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(jLabel4)
                                .addComponent(txtNoPenerimaan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Master", jPanel1);

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N
        jToolBar1.setPreferredSize(new java.awt.Dimension(100, 30));

        btnSave.setText("Save");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setName("btnSave"); // NOI18N
        btnSave.setPreferredSize(new java.awt.Dimension(100, 29));
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        jButton1.setText("Cancel");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(100, 29));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        btnBrowse.setText("Browse");
        btnBrowse.setFocusable(false);
        btnBrowse.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBrowse.setName("btnBrowse"); // NOI18N
        btnBrowse.setPreferredSize(new java.awt.Dimension(100, 29));
        btnBrowse.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });
        jToolBar1.add(btnBrowse);

        btnDo.setText("Print");
        btnDo.setFocusable(false);
        btnDo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDo.setName("btnDo"); // NOI18N
        btnDo.setPreferredSize(new java.awt.Dimension(100, 29));
        btnDo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDo);

        jTabbedPane2.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        jTabbedPane2.setName("jTabbedPane2"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        tableOrder.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Kode BRG", "Nama", "Qty", "Satuan", "Harga", "Disc(%)", "Discount", "Pajak", "Sub Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableOrder.setName("tableOrder"); // NOI18N
        tableOrder.setRowHeight(24);
        jScrollPane3.setViewportView(tableOrder);

        jTabbedPane2.addTab("Daftar Item", jScrollPane3);

        jPanel3.setName("jPanel3"); // NOI18N

        jLabel7.setText("Sub Total");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText("Pajak");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText("Discount");
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText("Total");
        jLabel10.setName("jLabel10"); // NOI18N

        txtSubTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtSubTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtSubTotal.setText("0");
        txtSubTotal.setName("txtSubTotal"); // NOI18N

        txtPajak.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtPajak.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtPajak.setText("0");
        txtPajak.setName("txtPajak"); // NOI18N

        txtTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        txtTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtTotal.setText("0");
        txtTotal.setName("txtTotal"); // NOI18N

        txtDiscount.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtDiscount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtDiscount.setText("0");
        txtDiscount.setName("txtDiscount"); // NOI18N

        jLabel11.setText("Keterangan");
        jLabel11.setName("jLabel11"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        txtKeterangan.setColumns(20);
        txtKeterangan.setRows(5);
        txtKeterangan.setText("-");
        txtKeterangan.setName("txtKeterangan"); // NOI18N
        jScrollPane2.setViewportView(txtKeterangan);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSubTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)
                            .addComponent(txtDiscount, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)
                            .addComponent(txtTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtPajak, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)))
                    .addComponent(jLabel8))
                .addGap(425, 425, 425)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtDiscount))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtSubTotal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtPajak))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtTotal))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 931, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 907, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        penerimaanDetailModels = new ArrayList<PenerimaanDetailModel>();

        penerimaanModel = new PenerimaanModel();
        for(PembelianDetailModel pdm : detailPembelianModels){
            penerimaanDetailModel = new PenerimaanDetailModel();
            penerimaanDetailModel.setBarang(pdm.getBarang());
            penerimaanDetailModel.setGudang(gudangModel);
            penerimaanDetailModel.setKeterangan("");
            penerimaanDetailModel.setNoKontainer("1");
            penerimaanDetailModel.setQty(pdm.getQty());
//            penerimaanDetailModel.setBulanKadaluarsa("201202");
            penerimaanDetailModels.add(penerimaanDetailModel);
        }

        penerimaanModel.setIdPembelian(pembelianModel.getIdPembelian());
        penerimaanModel.setKeterangan(txtKeterangan.getText());

        ReferenceNumberingModel rf = masterService.getReferenceNumber(2, "RCV");
        txtNoPenerimaan.setText(rf.getResult());
        penerimaanModel.setNomorPenerimaan(txtNoPenerimaan.getText());
        penerimaanModel.setNomorPo(pembelianModel.getNomorPo());
        penerimaanModel.setTanggal(dtPenerimaan.getDate());

        Set<PenerimaanDetailModel> set = new HashSet(penerimaanDetailModels);
        penerimaanModel.setPenerimaanDetailModels(set);

        try{
            PersistStatus status = penerimaanService.setPenerimaan(penerimaanModel);
            JOptionPane.showMessageDialog(null, status.getStatus());
            if (status.getStatus().equals("success")){
                btnSave.setEnabled(false);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Penyimpanan gagal.");
        }
}//GEN-LAST:event_btnSaveActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        clearForm();
}//GEN-LAST:event_jButton1ActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_btnBrowseActionPerformed

    private void btnDoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_btnDoActionPerformed

    private void btnGudangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGudangActionPerformed
        // TODO add your handling code here:
        gudangModel = new LookUpGudang(null, closable).getGudangModel();
        if(gudangModel != null)
            txtGudang.setText(gudangModel.getNamaGudang());
    }//GEN-LAST:event_btnGudangActionPerformed

    private void btnOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrderActionPerformed
        // TODO add your handling code here:
        pembelianModel = new LookUpOrderPembelian(null, closable).getPembelianModel();
        if(pembelianModel != null){
            txtBLNumber.setText(pembelianModel.getNomorKontrakPembelian());
            txtNoPo.setText(pembelianModel.getNomorPo());
            txtSupplier.setText(pembelianModel.getSuplier().getNamaSupplier());
            dtPenerimaan.setDate(new Date());

            detailPembelianModels = new ArrayList<PembelianDetailModel>(pembelianModel.getPembelianDetailModels());
            refreshTable();
        }
        
    }//GEN-LAST:event_btnOrderActionPerformed

    private void clearForm(){
        btnSave.setEnabled(true);
        penerimaanDetailModels = new ArrayList<PenerimaanDetailModel>();
        detailPembelianModels = new ArrayList<PembelianDetailModel>();
        p = new PembelianModel();

        txtNoPo.setText("");
        txtNoPenerimaan.setText(""); txtBLNumber.setText(""); txtGudang.setText("");
        dtPenerimaan.setDate(null);
        refreshTable();
        refreshTotalLabel();
    }

    private class PembelianTableModel extends AbstractTableModel{
        private List<PembelianDetailModel> detailPembelianModels;

        public PembelianTableModel(List<PembelianDetailModel> detailPembelianModels) {
            this.detailPembelianModels = detailPembelianModels;
        }

        @Override
        public int getRowCount() {
            return detailPembelianModels.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
                PembelianDetailModel s = detailPembelianModels.get(rowIndex);
                if (columnIndex == 0){
                    if (s.getBarang() != null)
                        return s.getBarang().getKdBarang();
                }else if (columnIndex == 1){
                    if (s.getBarang() != null)
                        return s.getBarang().getNamaBarang();
                }else if (columnIndex == 2){
                    if (s.getQty() != null)
                        return s.getQty();
                }else if (columnIndex == 3){
//                     if (s.getBarang().getSatuan() != null)
//                        return s.getSatuan().getBarang().getSatuan();
                }else if (columnIndex == 4){
                     if (s.getPrice() != null)
                        return s.getPrice();
                }else if (columnIndex == 5){
                    if (s.getDiscount2() != null)
                        return s.getDiscount2();
                }else if (columnIndex == 6){
                    if (s.getDiscount() != null)
                        return s.getDiscount();
                }else if (columnIndex == 7){
                    if (s.getPajak() != null)
                        return s.getPajak();
                }else if (columnIndex == 8){
                    if (s.getSubTotal() != null)
                        return s.getSubTotal();
                }
                return "";
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == 2) {
                return true;
            }
            return false;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            PembelianDetailModel s = detailPembelianModels.get(rowIndex);
            if (columnIndex == 0){
                s.setBarang((BarangModel) aValue);
//                s.setSatuan(s.getBarang().getSatuan());
//                s.setPrice(s.getBarang().getHargaJual());
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount2()).setScale(2));
                if(!s.getPajak().setScale(0, RoundingMode.HALF_DOWN).toString().equals("0"))
                    s.setPajak(s.getSubTotal().multiply(new BigDecimal(0.1)));
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }else if(columnIndex == 2){
                s.setQty(Integer.parseInt(aValue.toString()));
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount()).setScale(2));
                if(!s.getPajak().setScale(0, RoundingMode.HALF_DOWN).toString().equals("0"))
                    s.setPajak(s.getSubTotal().multiply(new BigDecimal(0.1)));
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }else if (columnIndex == 4){
                s.setPrice((BigDecimal) aValue);
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount()).setScale(2));
                if(!s.getPajak().setScale(0, RoundingMode.HALF_DOWN).toString().equals("0"))
                    s.setPajak((s.getSubTotal().multiply(new BigDecimal(0.1))));
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }else if (columnIndex == 5){
                s.setDiscount2(new BigDecimal(aValue.toString()));
                s.setDiscount(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).multiply(s.getDiscount2().divide(new BigDecimal(100))).setScale(2));
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount()).setScale(2));
                if(!s.getPajak().setScale(0, RoundingMode.HALF_DOWN).toString().equals("0"))
                    s.setPajak((s.getSubTotal().multiply(new BigDecimal(0.1))));
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }
            refreshTable();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }


    }

    private void refreshTable(){
        tableOrder.setModel(new PembelianTableModel(detailPembelianModels));
        refreshTotalLabel();
    }

    private void refreshTotalLabel(){
        if(detailPembelianModels !=null && !detailPembelianModels.isEmpty()){
            BigDecimal total = BigDecimal.ZERO;
            BigDecimal pajak = BigDecimal.ZERO;
            BigDecimal discount = BigDecimal.ZERO;
            for (PembelianDetailModel detailModel : detailPembelianModels) {
                total = total.add(detailModel.getSubTotal());
                pajak = pajak.add(detailModel.getPajak());
                discount = discount.add(detailModel.getDiscount());
            }
            txtSubTotal.setText(TextComponentUtils.formatNumber(total));
            txtPajak.setText(TextComponentUtils.formatNumber(pajak));
            txtDiscount.setText(TextComponentUtils.formatNumber(discount));

            txtTotal.setText(TextComponentUtils.formatNumber(total.add(pajak)));
        }else{
            txtSubTotal.setText("0");
            txtDiscount.setText("0");
            txtPajak.setText("0");
            txtTotal.setText("0");
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnDo;
    private javax.swing.JButton btnGudang;
    private javax.swing.JButton btnOrder;
    private javax.swing.JButton btnSave;
    private com.toedter.calendar.JDateChooser dtPenerimaan;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tableOrder;
    private javax.swing.JTextField txtBLNumber;
    private javax.swing.JLabel txtDiscount;
    private javax.swing.JTextField txtGudang;
    private javax.swing.JTextArea txtKeterangan;
    private javax.swing.JTextField txtNoPenerimaan;
    private javax.swing.JTextField txtNoPo;
    private javax.swing.JLabel txtPajak;
    private javax.swing.JLabel txtSubTotal;
    private javax.swing.JTextField txtSupplier;
    private javax.swing.JLabel txtTotal;
    // End of variables declaration//GEN-END:variables

}
